package com.ldzg.blockwatcher.watcher.tron;

import com.ldzg.blockwatcher.config.tron.TronGridClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 *  波厂链 触发器
 *
 * @author 18310740596@163.com
 * @version V1.0.0
 * @since 2025-07-03 08:58:40
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class TronBlockTrigger {

    private final TronGridClient tronGridClient;
    /**
     *  黑名单监听
     */
    private final Lock blacklistLock = new ReentrantLock();
    private final Lock transferLock = new ReentrantLock();
    /**
     *  黑名单监听时间
     */
    private final AtomicReference<Long> blacklistTime = new AtomicReference<>(0L);

    /**
     *  交易监听时间
     */
    private final AtomicReference<Long> transferTime = new AtomicReference<>(0L);

    /**
     *  15秒执行一次  监听冻结地址， 15秒运行一次
     *
     *  <p>
     *      如果项目部署多个节点, 请在这个任务触发器里面加上 分布式锁(Redisson 的 tryLock来限制只能同时有一个节点运行这个触发器)
     *  </p>
     *
     */
    @Async("asyncTaskExecutor")
    @Scheduled(cron = "0/15 * * * * ?")
    public void blackListTrigger() {
        boolean tryLock = blacklistLock.tryLock();
        if (tryLock) {
            if (Objects.isNull(blacklistTime.get())) {
                // 如果需要使用亚洲时区或者其他时区, 请修改此处
                blacklistTime.set(System.currentTimeMillis());
            }
            final long listenerInterval = 15;
            try {
                 long prev = this.blacklistTime.get() - listenerInterval;

                tronGridClient.queryAddedBlackListEvent(prev, this.blacklistTime.get());

            } catch (Exception e) {
                log.error("波场链触发器异常", e);
            } finally {
                blacklistLock.unlock();
                // 保证下次执行时间区间为相差15s
                this.blacklistTime.set( this.blacklistTime.get() + listenerInterval );
            }
        } else {
            log.info("TRON冻结事件正在执行中......");
        }
    }

    @Async("asyncTaskExecutor")
    @Scheduled(cron = "0/30 * * * * ?")
    public void transferTrigger() {
        boolean tryLock = transferLock.tryLock();
        if (tryLock) {
            if (transferTime.get() <= 0) {
                // 如果需要使用亚洲时区或者其他时区, 请修改此处
                transferTime.set(System.currentTimeMillis());
            }
            final long listenerInterval = 30000;

            try {
                long prev = this.transferTime.get() - listenerInterval;


                tronGridClient.getLatestTransfer(prev, this.transferTime.get());

            } catch (Exception e) {
                log.error("波场链触发器异常", e);
            } finally {
                transferLock.unlock();
                // 保证下次执行时间区间为相差30s
                this.transferTime.set( this.transferTime.get() + listenerInterval);
            }
        } else {
            log.info("Tron交易事件正在执行中......");
        }
    }
}


